package gof23.structure.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * JDK动态代理
 * @author zhiyizha
 *
 */
public class JDKDynamicProxyDemo {

	public static void main(String[] args) {
		Class<?>[] interfaces = RealSubject.class.getInterfaces();
		ClassLoader loader = RealSubject.class.getClassLoader();
		Subject proxySubject = (Subject)Proxy.newProxyInstance(loader,interfaces, new InvocationHandler() {
			private RealSubject proxySubject = new RealSubject();
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				System.out.println("before doSomthing ");
				Object returnValue = method.invoke(proxySubject, args);
				System.out.println("after doSomthing ");
				return returnValue;
			}
		});
		proxySubject.doSomthing();
		
	}
	
	
}